import { Controller } from 'egg';

class ActivityController extends Controller {
    /**
     * 搜索活动列表
     */
    public async searchActivityList() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.activityService.searchActivityList(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 根据条件搜索活动列表-微信
     */
    public async searchWxActivityList() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
                activity_nature: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const user_id = request.body.user_id;
        const school_id = request.body.school_id;
        if (user_id) {
            if (!school_id) {
                ctx.body = ctx.helper.responseErrorParam('学校id不能为空');
                return;
            }
        }
        const result = await ctx.service.activityService.searchWxActivityList(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 根据id查找活动详情
     */
    public async searchActivityById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                activity_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.activityService.searchActivityById(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 启动、暂停活动
     */
    public async startAndStopActivity() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                activity_id: 'string',
                type: 'type',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.activityService.startAndStopActivity(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 启动、暂停活动-微信
     */
    public async startAndStopWxActivity() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                activity_id: 'string',
                user_id: 'string',
                type: 'type',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.activityService.startAndStopWxActivity(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 结束活动-微信
     */
    public async endingActivity() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                activity_id: 'string',
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.activityService.endingActivity(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 结束活动-后台
     */
    public async endingAdminActivity() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                activity_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.activityService.endingAdminActivity(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 创建活动
     */
    public async createActivity() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                activity_title: 'string',
                activity_sponsors: 'string',
                activity_sponsors_phone: 'string',
                activity_nature: 'string',
                activity_start_time: 'string',
                activity_end_time: 'string',
                activity_place: 'string',
                activity_detail: 'string',
                activity_cover: 'string',
                activity_type: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const activity_type = Number(request.body.user_id);
        if (activity_type === 2) {
            if (!request.body.user_id) {
                ctx.body = ctx.helper.responseErrorParam('用户id不能为空');
                return;
            }
            if (!request.body.activity_code) {
                ctx.body = ctx.helper.responseErrorParam('验证码不能为空');
                return;
            }
        }
        await ctx.service.activityService.createActivity(request.body);
        ctx.body = ctx.helper.responseSuccess('添加成功');
    }

    /**
     * 创建活动
     */
    public async createWxActivity() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                activity_title: 'string',
                activity_sponsors: 'string',
                activity_sponsors_phone: 'string',
                activity_nature: 'string',
                activity_start_time: 'string',
                activity_end_time: 'string',
                activity_place: 'string',
                activity_detail: 'string',
                activity_cover: 'string',
                activity_type: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const activity_type = Number(request.body.user_id);
        if (activity_type === 2) {
            if (!request.body.user_id) {
                ctx.body = ctx.helper.responseErrorParam('用户id不能为空');
                return;
            }
            if (!request.body.activity_code) {
                ctx.body = ctx.helper.responseErrorParam('验证码不能为空');
                return;
            }
        }
        await ctx.service.activityService.createWxActivity(request.body);
        ctx.body = ctx.helper.responseSuccess('添加成功');
    }

    /**
     * 根据学校名模糊查找学校
     */
    public async searchSchoolByName() {
        const { ctx, ctx: { request } } = this;
        const result = await ctx.service.activityService.searchSchoolByName(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 获取每日活动发起数
     */
    public async getActivityNum() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                startTime: 'string',
                endTime: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.activityService.getActivityNum(request.body);
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }

    /**
     * 活动报名
     */
    public async activitySignIn() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                activity_id: 'string',
                user_id: 'string',
                type: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const { type, phone, code, weChat_account } = request.body;
        if (type == 1) {
            if (!phone || !code || !weChat_account) {
                ctx.body = ctx.helper.responseErrorParam('报名时请输入手机号、验证码、微信号等参数');
                return;
            }
        }
        await ctx.service.activityService.activitySignIn(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 谢绝参加
     */
    public async activityReject() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                activity_enroll_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.activityService.activityReject(request.body);
        ctx.body = ctx.helper.responseSuccess('操作成功');
    }

    /**
     * 根据用户id查找用户创建的活动以及参与的活动列表
     */
    public async searchActivityListById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        let result = await ctx.service.activityService.searchActivityListById(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 查找新活动数
     */
    public async searchReactActivityNum() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
                school_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        let result = await ctx.service.reactActivityService.searchReactActivityNum(request.body);
        ctx.body = ctx.helper.responseSuccess('查询成功', result);
    }

    /**
     * 清除新活动数
     */
    public async clearReactActivityNum() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
                school_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.reactActivityService.clearReactActivityNum(request.body);
        ctx.body = ctx.helper.responseSuccess('清除成功');
    }

    /**
     * 审核活动
     */
    public async auditActivity() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                activity_id: 'string',
                type: 'type'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.activityService.auditActivity(request.body);
        ctx.body = ctx.helper.responseSuccess('审核成功');
    }
}
export default ActivityController;